package com.shareinfo.adminManage.auto.main;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.shareinfo.adminManage.auto.model.AutoDbModel;
import com.shareinfo.adminManage.auto.model.AutoDbTableFieldModel;
import com.shareinfo.adminManage.auto.model.AutoDbTableModel;
import com.shareinfo.adminManage.auto.query.AutoDbTableQuery;
import com.shareinfo.adminManage.auto.service.AutoDbService;
import com.shareinfo.adminManage.auto.service.AutoDbTableFieldService;
import com.shareinfo.adminManage.auto.service.AutoDbTableService;
import com.shareinfo.adminManage.auto.service.TableService;
import com.shareinfo.auto.vo.ColumnInfo;
import com.shareinfo.auto.vo.ConfigureDatabase;
import com.shareinfo.auto.vo.ConfigureTemplate;
import com.shareinfo.auto.vo.ResultInfo;
import com.shareinfo.auto.vo.TableInfo;
import com.shareinfo.base.common.file.FileUtil;
import com.shareinfo.base.pagination.PageInfo;

/**
 * 
 * @author Administrator
 *
 */
public class TemplateBaseSmk {
	
	private static Logger log=Logger.getLogger(TemplateBaseSmk.class);

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-*.xml");
		
		String dbId="12ad0990-a07e-11e4-86e0-5404a6a5a8ae";//企信通
		String tableName="auto_db".toUpperCase();//表大写，oracle中判断时为大写的
		
		//说明表没有在数据库中,需要加载表的相关信息
		TableService tableService=(TableService)context.getBean("tableService");
		//数据库服务
		AutoDbService autoDbService=(AutoDbService) context.getBean("autoDbService");
		//数据库表服务
		AutoDbTableService autoDbTableService=(AutoDbTableService) context.getBean("autoDbTableService");
		//数据库字段服务
		AutoDbTableFieldService autoDbTableFieldService=(AutoDbTableFieldService)context.getBean("autoDbTableFieldService");
		
		AutoDbModel autoDbModel = autoDbService.getById(dbId);
		//1、判断数据库是否能够正常连接
		if(!tableService.ifCanConnection()){
			log.error("数据库连接失败");
			return;
		}
		log.debug("可建立数据库连接");
		ResultInfo resultInfo=tableService.getTableInfo(tableName);
		if(!resultInfo.isResult()){
			log.error(tableName+"表不存在");
			return;
		}
		//2、判断数据库表是否存在；存在则写入到auto_db_table和auto_db_table_field表中
		AutoDbTableQuery autoDbTableQuery=new AutoDbTableQuery();
		autoDbTableQuery.setDbId(dbId);
		autoDbTableQuery.setTableName(tableName);
		PageInfo<AutoDbTableModel> pageInfo= autoDbTableService.selectList(autoDbTableQuery);
		if(pageInfo.getTotalRecord()==0){
			//一、初始化设置表信息
			AutoDbTableModel autoDbTableModel=new AutoDbTableModel();
			autoDbTableModel.setDbId(dbId);
			autoDbTableModel.setTableName(tableName);
			autoDbTableModel.setDbClassId("0");
			//设置需要访问的数据库
			tableService.setAutoDbModel(autoDbModel);
			//设置表的注释
			if(StringUtils.isEmpty(resultInfo.getObj().toString())){
				autoDbTableModel.setTableComments(tableName);
			}else{
				autoDbTableModel.setTableComments(resultInfo.getObj().toString());
			}
			//二、获取主键
			resultInfo=tableService.getKeyColumn(tableName);
			if(!resultInfo.isResult()){
				log.error(resultInfo.getMsg());
				return;
			}
			//设置主键数、主键名称。多主键名称中间是用_分隔
			List<String> keyColumnList=(List<String>)resultInfo.getObj();
			autoDbTableModel.setTablePkNum(keyColumnList.size());
			String tablePkValue="";
			for (int i = 0; i < keyColumnList.size(); i++) {
				tablePkValue+=keyColumnList.get(i);
				if(i!=keyColumnList.size()-1)
					tablePkValue+="_";
			}
			autoDbTableModel.setTablePkName(tablePkValue);
			//保存表信息到数据库
			autoDbTableService.save(autoDbTableModel);
			//三、获取表字段
			resultInfo=tableService.getColumns(tableName);
			if(!resultInfo.isResult()){
				log.error(resultInfo.getMsg());
				return;
			}
			//字段列表
			List<ColumnInfo> listColumn = (List<ColumnInfo>)resultInfo.getObj();
			ColumnInfo columnInfo=null;
			List<AutoDbTableFieldModel> list = new ArrayList<AutoDbTableFieldModel>();
			AutoDbTableFieldModel autoDbTableFieldModel=null;
			for (int i = 0; i < listColumn.size(); i++) {
				autoDbTableFieldModel=new AutoDbTableFieldModel();
				columnInfo=listColumn.get(i);
				autoDbTableFieldModel.setDbTableId(autoDbTableModel.getId());
				autoDbTableFieldModel.setSqlColumnName(columnInfo.getSqlColumnName());
				autoDbTableFieldModel.setSqlDataType(columnInfo.getSqlDataType()+"");
				autoDbTableFieldModel.setSqlColumnSize(columnInfo.getSqlColumnSize()+"");
				autoDbTableFieldModel.setSqlDecimalDigits(columnInfo.getSqlDecimalDigits()+"");
				autoDbTableFieldModel.setSqlNullAble(columnInfo.getSqlNullAble()+"");
				autoDbTableFieldModel.setSqlRemarks(columnInfo.getSqlRemarks());
				autoDbTableFieldModel.setSqlColumnDef(columnInfo.getSqlColumnDef());
				autoDbTableFieldModel.setClassColumnName(columnInfo.getClassColumnName());
				autoDbTableFieldModel.setClassDataType(columnInfo.getClassDataType());
				//autoDbTableFieldService.save(autoDbTableFieldModel);
				list.add(autoDbTableFieldModel);
			}
			System.out.println(autoDbTableModel);
		}
		
		
		//包路径,类名称,表名,描述,请求路径（正式路径为请求路径+类名称）,
		/*String[][] table={
			{"","","","",""}	
			,{"com.shareinfo.adminManage.sys","SysMenu","sys_menu","系统菜单","/adminManage/sys"}
		};
		ConfigureInit conf = new ConfigureInit();
		conf.setConfigureTemplate(getConfigureTemplate());
		conf.setConfigureDatabase(getConfigureDatabase());
		
		for (int i = 0; i < table.length; i++) {
			if("".equals(table[i][0]))
				continue;
			conf.setPackageName(table[i][0]);
			conf.setClassName(table[i][1]);
			conf.setTableName(table[i][2].toUpperCase());
			conf.setComments(table[i][3]);
			conf.setUrlPath(table[i][4]);
			
			resultInfo = autoBuildService.run(conf);
			System.out.println(resultInfo);
		}	*/

	}

	// 获取配置模板
	public static ConfigureTemplate getConfigureTemplate() {
		ConfigureTemplate configureTemplate = new ConfigureTemplate();
		configureTemplate.setTemplateId("2");
		configureTemplate.setPath("H:/jobCode/git/hpAutoBuild/auto/src/main/webapp/sysTemplate/2");
		configureTemplate.setOutPath("D:/output");
		if(true){
			new FileUtil().deleteDirectory(configureTemplate.getOutPath());
		}
		return configureTemplate;
	}

	// 获取模板数据库
	public static ConfigureDatabase getConfigureDatabase() {
		ConfigureDatabase configureDatabase = new ConfigureDatabase();
		configureDatabase.setDriver("com.mysql.jdbc.Driver");
		configureDatabase.setUrl("jdbc:mysql://localhost:3306/smk?useUnicode=true&characterEncoding=UTF-8");
		configureDatabase.setUsername("root");
		configureDatabase.setPassword("root");
		configureDatabase.setSchema(null);
		configureDatabase.setCatalog(null);
		return configureDatabase;
	}
}
